# -*- coding = utf-8 -*-
from flask import Flask,jsonify,render_template,request,escape
from panduan import panduan,xieru
from gaode import geocode,weatherinfo,staticmap
import requests,json
import pandas as pd
from PIL import Image
from io import BytesIO
import direction_of_bicycling
xu_key = "9bce68c83a431dbc61cacc81a1de087b"

app = Flask(__name__)



def log_request(req:"flask_request",res:str):
	with open("view.log","a") as log:
		print(req.form,req.remote_addr,req.user_agent,res,file=log,sep='|')


# 起始界面
@app.route("/",methods = ["GET"])
def base() ->"html":
	return render_template("base.html")

# 登录界面
@app.route("/login",methods = ["GET"])
def login():
	return render_template("login.html")

# 注册界面
@app.route("/registered1",methods = ["GET"])
def registered_zhuce() ->"html":
	return render_template("registered1.html")

# 注册成功界面
@app.route("/registeredok1",methods = ["POST"])
def registered_ok() ->"html":
	zhuce_zhanghao = request.form["zhuce-zhanghao"]
	zhuce_mima = request.form["zhuce-mima"]
	xieru(zhuce_zhanghao,zhuce_mima)
	return render_template("registeredok1.html")

# 功能选择界面
@app.route("/start",methods = ["POST"])
def start():
	zhanghao = request.form["zhanghao"]
	mima = request.form["mima"]
	zhanghao=int(zhanghao)
	mima = int(mima)
	a = panduan(zhanghao,mima)
	log_request(request,a)
	if a == "true":
		return render_template("start.html")
	else:
		return render_template("loginfail.html",loginfail = "登录失败")

# 天气查询界面
@app.route("/tianqi",methods = ["POST"])
def yangshi():
	return render_template("tianqi.html")

# 天气查询成功界面
@app.route("/search",methods = ["POST"])
def hello_login():
	maincity = request.form["city"]
	tianqi = weatherinfo(maincity)
	province1 = tianqi["lives"][0]['province']
	city1 = tianqi["lives"][0]['city']
	adcode1 = tianqi["lives"][0]['adcode']
	weather1 = tianqi["lives"][0]['weather']
	temperature1 = tianqi["lives"][0]['temperature']
	winddirection1 = tianqi["lives"][0]['winddirection']
	windpower1 = tianqi["lives"][0]['windpower']
	humidity1 = tianqi["lives"][0]['humidity']
	reporttime1 = tianqi["lives"][0]['reporttime']
	return render_template(
		"tianqijieguo.html",
		the_geocode = tianqi,
		province = province1,
		city = city1,
		adcode = adcode1,
		weather = weather1,
		temperature = temperature1,
		winddirection = winddirection1,
		windpower = windpower1,
		humidity = humidity1,
		reporttime = reporttime1
		)

# 骑行路径查询界面
@app.route("/bicycle",methods = ["GET"])
def mapstart():
	return render_template("bicyclestart.html")

# 骑行路径生成界面
@app.route("/bicyclejieguo",methods = ["POST"])
def do_search_0() -> 'html':
    key = request.form['key']
    start_address = request.form['start_address']
    end_address = request.form['end_address']
    title = '查询结果'
    df = direction_of_bicycling.direction_bicycling(key,start_address, end_address)
    results = df.loc[:, ['instruction', 'distance', 'duration']]
    results.columns = ['指引', '路程', '时间']
    total_bicycle = direction_of_bicycling.total_bicycle(df)
    total_bicycle_time = direction_of_bicycling.total_bicycle_time(df)
    route = results['指引'].tolist()
    log_request(request, route)
    return render_template('bicyclejieguo.html',
                           the_title=title,
                           the_start_address=start_address,
                           the_end_address=end_address,
                           the_results=results,
                           the_html_table=results.to_html(index=False),
                           the_total_bicycle=total_bicycle,
                           the_total_bicycle_time=total_bicycle_time,)

# 日志界面
@app.route("/viewlog")
def view_log():
	with open("view.log","r") as log:
		contents = []
		for line in log:
			contents.append([])
			for item in line.split('|'):
				contents[-1].append(escape(item))
	titles = ["Form data","remote_addr","User_agent","Results"]
	return render_template("viewlog.html",
		the_data = contents,
		the_title = titles
		)




if __name__ == '__main__':
	app.run(debug=True)